package com.qqtech.qquan.user.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.user.enums.UserConst;
import com.qqtech.qquan.user.model.UserSysParam;
import com.qqtech.qquan.user.model.UserSysParamQuery;
import com.qqtech.qquan.user.model.UserSysParamVo;
import com.qqtech.qquan.user.service.UserSysParamService;

@Controller
@RequestMapping(value = "/user/sys/param")
public class UserSysParamController extends BaseAdminController{
	@Resource
	private UserSysParamService userSysParamService;

	@Override
	protected BaseService<UserSysParam> getBaseService() {
		return userSysParamService;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/user/sysParam/sysParam-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(UserSysParamQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		List<String> includeKeys=new ArrayList<String>();
		includeKeys.add(UserConst.PARAMKEY_USER_REGISTER_AWARD_START_TIME);
		includeKeys.add(UserConst.PARAMKEY_USER_REGISTER_AWARD_END_TIME);
		includeKeys.add(UserConst.PARAMKEY_USER_REGISTER_AWARD_VALUE);
		query.setIncludeKeys(includeKeys);
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<UserSysParamVo> page = userSysParamService.queryPageList(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	/**
	 * 去更新
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate")
	public String toUpdate(Integer id, Model model) {
		model.addAttribute("start_time", UserConst.PARAMKEY_USER_REGISTER_AWARD_START_TIME);
		model.addAttribute("end_time", UserConst.PARAMKEY_USER_REGISTER_AWARD_END_TIME);
		UserSysParamVo updateVo = userSysParamService.getById(id);
		model.addAttribute("updateVo", updateVo);
		return "/user/sysParam/sysParam-update";
	}
	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(UserSysParam updateVo) {
		Result ret = new Result();
		userSysParamService.updateById(updateVo);
		userSysParamService.clearAll();
		return ret;
	}
}
